package com.duoduo.demo.springnoxml.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.duoduo.demo.springnoxml.model.HelpTopic;

/**
 * 帮助Dao处理类
 * @author chengesheng@kedacom.com
 * @date 2017年10月23日下午3:08:55
 */
@Repository
public class HelpTopicDao extends BaseDao {

	public static final RowMapper<HelpTopic> entityRowMapper = new RowMapper<HelpTopic>() {

		@Override
		public HelpTopic mapRow(ResultSet rs, int rowNum) throws SQLException {
			HelpTopic entity = new HelpTopic();
			entity.setId(rs.getInt("help_topic_id"));
			entity.setName(rs.getString("name"));
			entity.setCategoryId(rs.getInt("help_category_id"));
			entity.setDescription(rs.getString("description"));
			entity.setExample(rs.getString("example"));
			entity.setUrl(rs.getString("url"));
			return entity;
		}
	};

	/**
	 * 获取所有帮助分类
	 */
	private static final String LIST_ALL = "select * from help_topic";

	public List<HelpTopic> listAll() {
		logger.debug("listAll {}", LIST_ALL);
		return super.getJdbcTemplate().query(LIST_ALL, entityRowMapper);
	}

	/**
	 * 获取指定分分类下的主题
	 */
	private static final String LIST_BY_CATEGORY = "select * from help_topic t where t.help_category_id=?";

	public List<HelpTopic> listByCategory(int categoryId) {
		logger.debug("listByCategory {}", LIST_BY_CATEGORY);
		return super.getJdbcTemplate().query(LIST_BY_CATEGORY, entityRowMapper, categoryId);
	}

	/**
	 * 获取指定关键字对应的主题
	 */
	private static final String LIST_BY_KEYWORD = "select * from help_topic t "
			+ "left join help_relation r on r.help_topic_id=t.help_topic_id left join help_keyword k on r.help_keyword_id=k.help_keyword_id "
			+ "where k.help_keyword_id=?";

	public List<HelpTopic> listByKeyword(int keywordId) {
		logger.debug("listByKeyword {}", LIST_BY_KEYWORD);
		return super.getJdbcTemplate().query(LIST_BY_KEYWORD, entityRowMapper, keywordId);
	}
}
